
/**
 * @file main.c
 * @author Morris (morris@mnblog.cn)
 * @brief
 * @version 0.0.1
 * @date 2022-01-17
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "loopbus_hal.h"
#include "acommon_err.h"
#include "alarm_type.h"
#include "bsp.h"
#include "loopbus.h"
#include "bsp_control.h"


//extern volatile Alter_unit_Val   Alter_Val;		//修改阈值

//static volatile uint8_t          flag_key;
//extern volatile sys_alarm_type_t alarm;
//extern volatile uint8_t led_disable;

extern volatile bk9000_unit_event_t sys_state;
extern volatile uint8_t             loopbus_connect;

static volatile uint8_t flag_key;
static volatile uint8_t rtc_flag;

#if 1
__USED int leds_pulse(uint16_t t1, uint16_t t2)
{
    if (sys_state.alarm) leds_set_pulse_out(t1, t2);
    return sys_state.legacy.mask.alarm;
}
__USED void leds_ping(uint8_t en) { en ? alarm_led_on() : alarm_led_off(); }

#else
__USED void leds_pulse(uint16_t t1, uint16_t t2) { leds_set_pulse_out(t1, t2); }
#endif


void rtc_interrupt_handle(void)
{
		static uint8_t led_cnt = 0;
    if (!loopbus_connect && sys_state.alarm && !(led_cnt = (led_cnt + 1) % 2)) { led_alarm_on_delay(50); }
    rtc_flag = 1;

}

#define RESET_COUNT 2
void loop(void)
{
//	if(!detector_state.cheek_on)
//		return ;
	/*模拟报警*/
	if(bsp_ScanfKey())
	{	
		if(key_count<4090)
		{	
		 #if SHYS_CONFIG_OUTSIDE
			if(key_count<=4)
			{
				alarm_led_on(); 
				bsp_delay_ms(50);
				alarm_led_off();
				bsp_delay_ms(50);	
			}
		#endif
			key_count++;
		}
		printf("The Key Is Down:%d\n\n",key_count);
		/*持续1分钟导通则报故障*/
		if(key_count>120)
		{
			#ifdef KEY_MODLE
			/*干簧管故障*/
			if(!relay.key_fault)
				relay.key_fault=1;
			#endif
		}
	}
	else
	{
		/*导通3秒~1分钟，松开后则触发模拟报警*/
		if(key_count>4&&key_count<120)
		{
			key_count=0;
			#ifdef KEY_MODLE
			if(!sys_state.alarm)
				sys_state.alarm=1;
			#endif
		}
		if(relay.key_fault)
			relay.key_fault=0;
		if(key_count)
			key_count=0;
	}
	static float ntc_temp=0;
//printf("temp_flag.start %d\n",temp_flag.start);
	/*1S一次*/
	if(temp_flag.start)
	{
		
		/*自主工作模式闪灯*/
		if(!led.run_Imodle)
		{
		 #if SHYS_CONFIG_OUTSIDE
			
			static  uint8_t run_led_cnt=0;
			run_led_cnt++;
			bsp_power_dowm_control(&bsp_power_lose_t);
			if(run_led_cnt>=10)
			{
				run_led_cnt=0;
				if(!led.run)
					led_run_on_delay(50); 
			}
		 #else   
			static uint8_t run_count=0;
			run_count++;
			if(run_count==10)
			{
				run_count=0;
				//led_run_on_delay(10);
			}
		 #endif
		}
		
		printf("MODLE: [%.2x]S[%d]R[%d]\nOFFSET:[%d]\n",work.modle,work.s,work.r,work.offset);	
		
		/*温度检测*/
		uint16_t temp_pt_ad=0;
		float temp_source=0;
		/*PT100温度读取*/
		if(bsp_PT100_Temp_Get(&temp_pt_ad,&temp_source,temp_magnification))
		{ 
			static uint8_t fa_count=0;
			if(fa_count<30)
				fa_count++;
			temp_data.source=temp_source;
			#ifdef LCCS_MODLE
			/*普通检测模式*/
			if(work.bk2)
			{
				temp_data.current=temp_data.source;
			}
			/*火灾探测模式*/
			else
			{
			#endif
				if(fa_count==30)
					temp_flag.speed_cheek=1;
				else
					temp_flag.speed_cheek=0;
				
				//<升温速率检测
				bsp_Temp_Speed_Cheek();
				//>test AD采集PT1000 原数据
				#if TEST_SHYS
				bus_thd_t.thd_t.temp=temp_source;
				//bus_thd_t.thd_t.co=temp_data.speed_rate*10;
				bus_thd_t.thd_t.co=((temp_source-(uint16_t)temp_source)*100);
				#endif
				//<
				bsp_Temp_Recension();	
			#ifdef LCCS_MODLE
			}
			#endif
			#ifndef TEST_SHYS
			bus_thd_t.thd_t.co=((temp_data.source-(uint16_t)temp_data.source)*100);
			#endif 
			if(count.fault)
				count.fault=0;
			if(temp_flag.fault)
				temp_flag.fault=0;
			
			/*报警检测,火警优先最高*/
			if(temp_data.current>=temp_data.alarm)
			{
				if(count.prepare_alarm)
					count.prepare_alarm=0;
				if(count.alarm<3)
					count.alarm++;
				if(count.alarm==3)
				{
					if(sys_state.pre_alarm)
						sys_state.pre_alarm=0;//上位机标志位
					if(!sys_state.alarm)
					{
						sys_state.alarm=1;//上位机标志位
						#ifdef EVENT_MODLE
						bsp_event_renew(EVENT_ALARM);//事件记录
						#endif
					}
				}
			}//报警检测end
			#ifdef LCCS_MODLE
				#define TEMP_PREPARE_MODLE OFF
			#else
				#define TEMP_PREPARE_MODLE ON
			#endif
			#if TEMP_PREPARE_MODLE
			/*预警检测*/
			else if(temp_data.current>=temp_data.prepare_alarm&&temp_data.current<temp_data.alarm)
			{
				if(count.alarm)
					count.alarm=0;
				if(count.prepare_alarm<3)
					count.prepare_alarm++;

				if(count.prepare_alarm==3)
				{
					if(alarm.alarm)
					{
						if(alarm.pre_alarm)
							alarm.pre_alarm=0;
					}
					else
					{
						if(!alarm.pre_alarm)
						{
							alarm.pre_alarm=1;//上位机标志位
							#ifdef EVENT_MODLE
							bsp_event_renew(EVENT_PREPARE_ALARM);//事件记录
							#endif
						}
					}
				}
			}
			#endif	//预警检测end
			else
			{
				if(sys_state.pre_alarm)
					sys_state.pre_alarm=0;
				if(count.alarm)
					count.alarm=0;
				if(count.prepare_alarm)
					count.prepare_alarm=0;
			}
			#if TEST_SHYS
			 bus_thd_t.thd_t.smoke=(int16_t)temp_data.current;	
			#else 
			//最终补偿后数据上传
			 bus_thd_t.thd_t.temp=temp_data.current;
			
			#endif
		}//采样正常end.
		/*故障处理*/
		else
		{
			bus_thd_t.thd_t.temp=1;//数据上传
			temp_data.current=0;
			count.speed=0;
			temp_data.speed_rate=0;
			#define TEMP_ERROR_COUNT 60
			if(count.fault<TEMP_ERROR_COUNT)
				count.fault++;
			if(count.fault==TEMP_ERROR_COUNT)
			{
				if(!temp_flag.fault)
				{
					temp_flag.fault=1;//上位机标志位
					
					#ifdef EVENT_MODLE
					bsp_event_renew(EVENT_FAULT);//事件记录
					#endif
				}
			}
		printf("fault:%d\n",count.fault);
		}
		printf("s_r:[%.3f]\ns_m:[%d]\n",temp_data.speed_rate,temp_speed_modle);
		printf("A  :[%d]\nPA :[%d]\nC  :[%.2f]\nS  :[%.2f]\nSR :[%.2f]\n",temp_data.alarm,temp_data.prepare_alarm
														,temp_data.current,temp_data.source,ntc_temp);
		printf("LOW:[%d]\n",temp_flag.low_start);
		#ifdef EVENT_MODLE
		/*报警前后模拟量记录*/
		bsp_alarm_anolog_envent(alarm.alarm,temp_data.current);
		#endif
		
		if(temp_flag.fault||relay.key_fault)
		{
			if(!sys_state.power_fault)
				sys_state.power_fault=1;
		}
		else
		{
			if(sys_state.power_fault)
				sys_state.power_fault=0;
		}
		#if SHYS_CONFIG_OUTSIDE
		/*显示灯*/
		bsp_led_control(sys_state);
		/*继电器动作*/
		bsp_Alarm_Mode_Switching();
		#else
		/*继电器动作*/
		bsp_Alarm_Mode_Switching();
		#endif
		printf("\na:[%d] pa:[%d] f:[%d]\n\n",sys_state.alarm,sys_state.pre_alarm,sys_state.power_fault);
		bus_thd_t.flag=1; 
		temp_flag.start=0;	
	}
	else
	{
		if(ntc.flag)
		{
			ntc_temp=bsp_NTC_Temp_Get();
			#if TEST_SHYS
			
			#else 
			bus_thd_t.thd_t.smoke=ntc_temp;	//板子上温度检测，放入总线阈值的smoke显示
			#endif
			ntc.flag=0;
		}
			
	}
}	//loop
